/**
 * Created by Aimee on 14-11-11.
 */
define(['jquery', 'jquerymobile', 'net', 'dialogs',  'activity_register'], function($, m, net, dia, activityRegister) {
    var m_activity = null;

    var m_from = null;

    // 补充0
    function pad(n, width, z) {
        z = z || '0';
        n = n + '';
        return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
    }

    function getMonthString(month) {
        month = month + 1;
        if (month == 1)
            return 'Jan';
        else if (month == 2)
            return 'Feb';
        else if (month == 3)
            return 'Mar';
        else if (month == 4)
            return 'Apr';
        else if (month == 5)
            return 'May';
        else if (month == 6)
            return 'June';
        else if (month == 7)
            return 'July';
        else if (month == 8)
            return 'Aug';
        else if (month == 9)
            return 'Sept';
        else if (month == 10)
            return 'Oct';
        else if (month == 11)
            return 'Nov';
        else if (month == 12)
            return 'Dec';
        else {
            console.assert(false, '月份 %o 不存在 ', month );
        }
    }

    //获取活动
    function getAcvtivities(m_activity) {
		//fixed show duplicate activity issue
		if($('#activities_details_listview_news li').length > 1){
			return;
		}
        var value = m_activity;
        //获取活动
        var startDate = new Date(value.startTime * 1000);
        var endDate = new Date(value.endTime * 1000);
        var startTime = 'Start: ' + pad(startDate.getHours(), 2) + ':' + pad(startDate.getMinutes(),2) + ', ' + startDate.getDate() + " " + getMonthString(startDate.getMonth()) + " " + startDate.getFullYear();
        var endTime = 'End: ' + pad(endDate.getHours(),2) + ':' + pad(endDate.getMinutes(),2) + ', ' + endDate.getDate() + " " + getMonthString(endDate.getMonth()) + " " + endDate.getFullYear();
        var venue = 'Venue: ' + value.venue;
        var li = creatActivity(value.banner, value.subject, value.description, startTime, endTime, venue, value.tags);
        $('#activities_details_listview_news').append(li);
    }

    //创建活动
    function creatActivity(img, subject, brifContent, time, day, venue, tags) {

        var html_tags = '';
        if(tags != null) {
            $.each(tags, function(index, val) {
                if (val.name == 'Communications')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(77, 134, 176);">COMM</span>  ';
                if (val.name == 'Corporate Sustainability')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(137, 217, 78);">CS</span>  ';
                if (val.name == 'Finance')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(219, 218, 0);">FIN</span>  ';
                if (val.name == 'Trade Union')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(147, 182, 164);">GTU</span>  ';
                if (val.name == 'Human Resources')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(230, 170, 0);">HR</span>  ';
                if (val.name == 'Innovation')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(76, 176, 170);">INNO</span>  ';
                if (val.name == 'Center Operations')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(138, 116, 122);">OPS</span>  ';
                if (val.name == 'Technical Development')
                    html_tags += '\<span class="spanTag" style="background-color: rgb(121, 110, 172);">TD</span>  ';
            });
        }

        // Nick removed for multiple images start
        // var html = '\<li style="border: 0px;padding: 0px;margin-bottom:10px;background-color: white;">\
        //     <div id="detail_sele" style="overflow: auto;">\
        //         <div style="width: 100%">\
        //             <img src="'+img+'"\ style="width: 100%;" alt=""/>\
        //             <div style="background: rgba(0,0,0,.5);margin-top: -24px;color: #FFFFFF;text-align: right;padding-right: 7px;position: relative;">Tap for more photos</div>\
        //         </div>\
        //         <label style="margin: 10px; font-size: 18px; white-space:normal;color: #404040">'+ subject +'\</label>\
        //         <div style="margin: 10px">\
        //             <div style="white-space:normal;font-weight: normal;font-size: 12px;color: #404040">'+ brifContent.replace(/\n/g,'</br>') +'\</div>\
        //         </div>\
        //         <div style="margin: 10px;font-size: 11px;color: #404040;">\
        //             <span style="display: block;white-space:normal;">'+ time +'\</span>\
        //             <span style="display: block;white-space:normal;">'+ day +'\</span>\
        //             <span style="display: block;white-space:normal;">'+ venue +'\</span>\
        //         </div>\
        //         <div style="border-bottom: solid 1px;border-bottom-color: #C0C0C0;width: 100%"></div>\
        //         <div style="margin-left: 10px;width: auto;white-space: normal;">\
        //             '+ html_tags +'\
        //         \</div>\
        //         <div style="clear:left; width: 100%;height:10px;"></div>\
        //     </div>\
        //     </li>';
        // Nick removed for multiple images end
        // Nick added for multiple images start
        var morePhotosLabel = m_activity.imageList.length > 0 ? '\<div style="background: rgba(0,0,0,.5);margin-top: -24px;color: #FFFFFF;text-align: right;text-shadow: none;padding-right: 7px;position: relative;font-size: 15px;height: 20px;line-height: 20px;">Tap for more photos\</div>' : '';
            html = '\<li style="border: 0px;padding: 0px;margin-bottom:10px;background-color: white;">\
            <div id="detail_sele" style="overflow: auto;">\
                <div style="width: 100%">\
                    <img src="'+img+'" style="width: 100%;" alt=""/>'
                    + morePhotosLabel + '\
                </div>\
                <label style="margin: 10px; font-size: 18px; white-space:normal;color: #404040">'+ subject +'\</label>\
                <div style="margin: 10px">\
                    <div style="white-space:normal;font-weight: normal;font-size: 12px;color: #404040">'+ brifContent.replace(/\n/g,'</br>') +'\</div>\
                </div>\
                <div style="margin: 10px;font-size: 11px;color: #404040;">\
                    <span style="display: block;white-space:normal;">'+ time +'\</span>\
                    <span style="display: block;white-space:normal;">'+ day +'\</span>\
                    <span style="display: block;white-space:normal;">'+ venue +'\</span>\
                </div>\
                <div style="border-bottom: solid 1px;border-bottom-color: #C0C0C0;width: 100%"></div>\
                <div style="margin-left: 10px;width: auto;white-space: normal;">\
                    '+ html_tags +'\
                </div>\
                <div style="clear:left; width: 100%;height:10px;"></div>\
            </div>\
            </li>';
        // Nick added for multiple images end
        return html;
    }



    //返回 显示所有活动的页面
    $('#activity_details_btn_back').off('click')
        .on('click', function() {
            console.assert(m_from != null, '没有指定从哪来的，所以回不去了。请调用from方法指定来路。');
            $.mobile.backChangePage(m_from ,{ transition: "slide",reverse: true, changeHash:false});
        });

    //跳转到注册页面
    $('#btn_details_registerNow').off('click')
        .on('click', function() {
            /*activityRegister.setAcvtivities(m_activity);
            activityRegister.from('#activity_detail');
            $.mobile.newChangePage('#activityRegister', { transition: "slide", reverse: false, changeHash:false});*/
        	var _activityFlag = m_activity['flag'];
            if(!_activityFlag){
                activityRegister.setAcvtivities(m_activity);
                activityRegister.from('#activity_detail');
                $.mobile.newChangePage('#activityRegister', { transition: "slide", reverse: false, changeHash:false});
            }else{
            	if(_activityFlag=='activity.deadTime'){
            		dia.alert("Oops!","You are too late! The registration of the activity is already closed.", ['OK'], function () {
    				         return false;
    				 });
            	}else if(_activityFlag=='activity.limitNum'){
            		dia.alert("Oops!","Sorry but your registration cannot be processed now as the activity has reached its maximum capacity.", ['OK'], function () {
    				         return false;
    				 });
            	}else if(_activityFlag=='activity.appIsTwo'){
            		dia.alert("Oops!","Looks like you have already registered this activity!", ['OK'], function () {
    			         return false;
    				 });
            	}else{
            		dia.alert("Oops!","Looks like have other issues. please contact #red help center!", ['OK'], function () {
    			         return false;
    				 });
            	}
            }
        });

    // 消失的时候删除活动
    $("#activity_detail").on( "pagehide", function( event ) {
        $('#activities_details_listview_news').empty();
    });

    //设置内容高度是Header＋button剩下的高度
    $("#activity_detail").on( "pageshow", function( event ) {
        window.setBodyOverflow($(document.body));
        $('#activities_details_content').css('height',($(window).height()-88));
        getAcvtivities(m_activity);
		
		$("#activities_details_listview_news img").off('click').on('click', function(){
		// Nick added for multiple images start
		if (m_activity.imageList.length > 0)
		// Nick added for multiple images end
			$.mobile.newChangePage('#page_multipulte_img',{ transition: "slide",reverse: false,changeHash: false})
		});
    });

    return {
        showDetailWithActivity : function (activity) {
            m_activity = activity;
            // Nick added for multiple images start
            window.imageList = m_activity.imageList;
            // Nick added for multiple images end
        },
        from : function (from) {
            m_from = from;
        }
    }
});